<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */
defined('IN_IA') or exit('Access Denied');
$dos = array('default', 'groupdata', 'post');
$do = in_array($do, $dos) ? $do : 'default';
if($do == 'default') {
	$accounts = uni_accounts($_W['uniacid']);
	if(!empty($accounts)) {
		$accdata = array();
		foreach($accounts as $account) {
			if($account['type'] == 1 && $account['type'] > 0) {
				$accdata[] = array('acid' => $account['acid'], 'name' => $account['name']);
			}
		}
	}


	$groups_data = pdo_fetchall('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
	if(!empty($groups_data)) {
		$groups = array();
		foreach($groups_data as $gr) {
			$groups[$gr['acid']] = iunserializer($gr['groups']);
		}
	}

	load()->func('tpl');
	template('mc/mass');
}

if($do == 'groupdata') {
	if($_W['isajax']) {
		$acid = intval($_GPC['acid']);
		$groups = pdo_fetch('SELECT * FROM ' . tablename('mc_fans_groups') . ' WHERE uniacid = :uniacid AND acid = :acid', array(':uniacid' => $_W['uniacid'], ':acid' => $acid));
		$groups = unserialize($groups['groups']) ? unserialize($groups['groups']) : array();
		if(empty($groups)) {
			exit(json_encode(array('status' => 'empty', 'message' => '该公众号还没有从公众平台获取粉丝分组')));
		} else {
			$html = '<option name="groupid" value="0">请选择粉丝分组</option><option value="-2" name="groupid">全部用户</option>';
			foreach($groups as $group) {
				if( $group['id'] == 0) {
					$group['id'] = -1;
				}
				$html .= '<option name="groupid" value="' . $group['id'] . '">' .  $group['name'] . '</option>';
			}
			exit(json_encode(array('status' => 'success', 'message' => $html)));
		}
	}
}

if($do == 'post') {
	if($_W['isajax']) {
		$acid = intval($_GPC['acid']);
		$groupid = intval($_GPC['groupid']);
		$msgtype = intval($_GPC['msgtype']);
		if($groupid == -1) {
			$groupid = 0;
		}
		if($msgtype != 6) {
			$formdata_tmp = explode('&', urldecode($_GPC['formdata']));
			if(!empty($formdata_tmp)) {
				$formdata = array();
				foreach($formdata_tmp as $formda) {
					$li = explode('=', $formda);
					$formdata[$li[0]] = $li[1];
				}
			}
		} else {
			if(!empty($_GPC['formdata'])) {
				foreach($_GPC['formdata'] as $key => $li_tmp) {
					$content = $_GPC['content'][$key];
					if(empty($content)) {
						continue;
					} else {
						$str_find = array('../attachment/images');
						$str_replace = array($_W['siteroot'] . 'attachment/images');
						$content =  str_replace($str_find, $str_replace, $content);
					}
					$formdata_tmp = explode('&', urldecode($li_tmp));
					if(!empty($formdata_tmp)) {
						foreach($formdata_tmp as $formda) {
							$li = explode('=', $formda);
							if(($li[0] == 'title' && $li[1] == '') || ($li[0] == 'thumb_media_id' && $li[1] == '')) {
								break;
							}
							if($li[0] == 'content_source_url' && !empty($li[1])) {
								if(!strexists('http://', $li[1]) && !strexists('https://', $li[1])) {
									$li[1] = $_W['siteroot'] . 'app/' . ltrim($li[1], '.');
								}
							}
							$formdata_tmp1[$li[0]] = urlencode($li[1]);
						}
						if(!empty($formdata_tmp1)) {
							$formdata_tmp1['content'] = urlencode($content);
							$formdata[] = $formdata_tmp1;
						}
					}
				}
			}
		}

		$send['filter'] = array('groupid' => $groupid);
		$send['filter']['is_to_all'] = 0;
		if($groupid == -2) {
			$send['filter']['is_to_all'] = 1;
			$send['filter']['groupid'] = 0;
		}
		if($msgtype == '7') {
			$send['msgtype'] = 'text';
			$send['text'] = array('content' => $formdata['content']);
		} elseif($msgtype == '2') {
			$send['msgtype'] = 'image';
			$send['image'] = array('media_id' => $formdata['media_id']);
		} elseif($msgtype == '3') {
			$send['msgtype'] = 'voice';
			$send['voice'] = array('media_id' => $formdata['media_id']);
						$data['media_id'] =  $formdata['media_id'];
			$data['title'] =  urlencode($formdata['title']);
			$data['description'] =  urlencode($formdata['description']);
			$acc = WeAccount::create($acid);
			$status = $acc->uploadVideo($data);
			if(is_error($status)) {
				exit(json_encode($status));
			}

			$send['msgtype'] = 'mpvideo';
			$send['mpvideo'] = array('media_id' => $status['media_id']);
		} elseif($msgtype == '6') {
						if(!empty($formdata)) {
				$data['articles'] = $formdata;
				$acc = WeAccount::create($acid);
				$status = $acc->uploadNews($data);
				if(is_error($status)) {
					exit(json_encode($status));
				}
			} else {
				exit(json_encode(error(-1, '没有有效的消息内容')));
			}
			$send['msgtype'] = 'mpnews';
			$send['mpnews'] = array('media_id' => $status['media_id']);
		}
		$acc = WeAccount::create($acid);
		$status = $acc->fansSendAll($send);
		if(is_error($status)) {
			exit(json_encode($status));
		} else {
			exit('success');
		}
	}
}
